
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 09/18/2011 18:55:21
-- Generated from EDMX file: D:\CoffeeTea\coffee-tea\CoffeeTeaTrading\CoffeeTeaTrading.UI\Models\Model1.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [CTData];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_CTCustomers_Address]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CTCustomers] DROP CONSTRAINT [FK_CTCustomers_Address];
GO
IF OBJECT_ID(N'[dbo].[FK_CTCustomers_Orders]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CTCustomers] DROP CONSTRAINT [FK_CTCustomers_Orders];
GO
IF OBJECT_ID(N'[dbo].[FK_OrderItems_Orders]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[OrderItems] DROP CONSTRAINT [FK_OrderItems_Orders];
GO
IF OBJECT_ID(N'[dbo].[FK_OrderItems_Products]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[OrderItems] DROP CONSTRAINT [FK_OrderItems_Products];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Address]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Address];
GO
IF OBJECT_ID(N'[dbo].[CTCustomers]', 'U') IS NOT NULL
    DROP TABLE [dbo].[CTCustomers];
GO
IF OBJECT_ID(N'[dbo].[OrderItems]', 'U') IS NOT NULL
    DROP TABLE [dbo].[OrderItems];
GO
IF OBJECT_ID(N'[dbo].[Orders]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Orders];
GO
IF OBJECT_ID(N'[dbo].[Products]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Products];
GO
IF OBJECT_ID(N'[dbo].[sysdiagrams]', 'U') IS NOT NULL
    DROP TABLE [dbo].[sysdiagrams];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Address'
CREATE TABLE [dbo].[Address] (
    [ID] int IDENTITY(1,1) NOT NULL,
    [Street] nvarchar(50)  NOT NULL,
    [House] int  NOT NULL,
    [Building] nvarchar(50)  NOT NULL
);
GO

-- Creating table 'CTCustomers'
CREATE TABLE [dbo].[CTCustomers] (
    [ID] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(50)  NOT NULL,
    [Number] int  NOT NULL,
    [AddressID] int  NOT NULL
);
GO

-- Creating table 'OrderItems'
CREATE TABLE [dbo].[OrderItems] (
    [ID] int IDENTITY(1,1) NOT NULL,
    [ItemsAmount] int  NOT NULL,
    [OrderID] int  NOT NULL,
    [ProductID] int  NOT NULL
);
GO

-- Creating table 'Orders'
CREATE TABLE [dbo].[Orders] (
    [ID] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(50)  NOT NULL,
    [Type] int  NOT NULL,
    [TotalAmount] int  NOT NULL,
    [OrderDate] datetime  NOT NULL,
    [Status] int  NOT NULL,
    [CTCustomerID] nvarchar(max)  NOT NULL,
    [CTCustomerID1] int  NOT NULL
);
GO

-- Creating table 'Products'
CREATE TABLE [dbo].[Products] (
    [ID] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(50)  NOT NULL,
    [Cost] real  NOT NULL,
    [ProductType] int  NOT NULL
);
GO

-- Creating table 'sysdiagrams'
CREATE TABLE [dbo].[sysdiagrams] (
    [name] nvarchar(128)  NOT NULL,
    [principal_id] int  NOT NULL,
    [diagram_id] int IDENTITY(1,1) NOT NULL,
    [version] int  NULL,
    [definition] varbinary(max)  NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [ID] in table 'Address'
ALTER TABLE [dbo].[Address]
ADD CONSTRAINT [PK_Address]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'CTCustomers'
ALTER TABLE [dbo].[CTCustomers]
ADD CONSTRAINT [PK_CTCustomers]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'OrderItems'
ALTER TABLE [dbo].[OrderItems]
ADD CONSTRAINT [PK_OrderItems]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Orders'
ALTER TABLE [dbo].[Orders]
ADD CONSTRAINT [PK_Orders]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Products'
ALTER TABLE [dbo].[Products]
ADD CONSTRAINT [PK_Products]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [diagram_id] in table 'sysdiagrams'
ALTER TABLE [dbo].[sysdiagrams]
ADD CONSTRAINT [PK_sysdiagrams]
    PRIMARY KEY CLUSTERED ([diagram_id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [AddressID] in table 'CTCustomers'
ALTER TABLE [dbo].[CTCustomers]
ADD CONSTRAINT [FK_CTCustomers_Address]
    FOREIGN KEY ([AddressID])
    REFERENCES [dbo].[Address]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CTCustomers_Address'
CREATE INDEX [IX_FK_CTCustomers_Address]
ON [dbo].[CTCustomers]
    ([AddressID]);
GO

-- Creating foreign key on [OrderID] in table 'OrderItems'
ALTER TABLE [dbo].[OrderItems]
ADD CONSTRAINT [FK_OrderItems_Orders]
    FOREIGN KEY ([OrderID])
    REFERENCES [dbo].[Orders]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_OrderItems_Orders'
CREATE INDEX [IX_FK_OrderItems_Orders]
ON [dbo].[OrderItems]
    ([OrderID]);
GO

-- Creating foreign key on [ProductID] in table 'OrderItems'
ALTER TABLE [dbo].[OrderItems]
ADD CONSTRAINT [FK_OrderItems_Products]
    FOREIGN KEY ([ProductID])
    REFERENCES [dbo].[Products]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_OrderItems_Products'
CREATE INDEX [IX_FK_OrderItems_Products]
ON [dbo].[OrderItems]
    ([ProductID]);
GO

-- Creating foreign key on [CTCustomerID1] in table 'Orders'
ALTER TABLE [dbo].[Orders]
ADD CONSTRAINT [FK_CTCustomerOrder]
    FOREIGN KEY ([CTCustomerID1])
    REFERENCES [dbo].[CTCustomers]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CTCustomerOrder'
CREATE INDEX [IX_FK_CTCustomerOrder]
ON [dbo].[Orders]
    ([CTCustomerID1]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------